<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />

    <van-loading
      size="24px"
      color="#0094ff"
      vertical
      v-if="Object.keys(ArticleObj).length == 0"
      >加载中...</van-loading
    >
    <!-- <van-loading
      size="24px"
      color="#0094ff"
      vertical
      v-if="ArticleObj.name === undefined"   //不能用
      >加载中...</van-loading
    > -->
    <div v-else>
      <!-- 文章信息区域 -->
      <div class="article-container">
        <!-- 文章标题 -->
        <h1 class="art-title">{{ ArticleObj.title }}</h1>

        <!-- 用户信息 -->
        <!-- 多少天之前 -->

        <van-cell
          center
          :title="ArticleObj.aut_name"
          :label="parseTime(ArticleObj.pubdate)"
        >
          <template #icon>
            <img :src="ArticleObj.aut_photo" alt="" class="avatar" />
          </template>
          <template #default>
            <div>
              <!-- 点解取消关注 -->
              <van-button
                type="info"
                size="mini"
                v-if="ArticleObj.is_followed"
                @click="follow(ArticleObj.art_id, false)"
                >已关注
              </van-button>
              <!-- 点解关注 -->

              <van-button
                icon="plus"
                type="info"
                size="mini"
                plain
                v-else
                @click="follow(ArticleObj.art_id, true)"
                >关注</van-button
              >
            </div>
          </template>
        </van-cell>

        <!-- 分割线 -->
        <van-divider></van-divider>

        <!-- 文章内容 -->
        <div class="art-content" v-html="ArticleObj.content"></div>

        <!-- 分割线 -->
        <van-divider>End</van-divider>

        <!-- 点赞 -->
        <div class="like-box">
          <van-button
            icon="good-job"
            type="danger"
            size="small"
            v-if="ArticleObj.attitude == 1"
            @click="likeArticle(false)"
            >已点赞</van-button
          >
          <van-button
            icon="good-job-o"
            type="danger"
            plain
            size="small"
            v-else
            @click="likeArticle(true)"
            >点赞</van-button
          >
        </div>
      </div>

      <commentListVue></commentListVue>
    </div>
  </div>
</template>

<script>
import {
  articleDetail,
  followingsUser,
  cancelFollowingsUser,
  likeArticle,
} from "@/api/article";
import commentListVue from "./components/commentList.vue";
import { timeAgo } from "@/utils/timeAgo";
export default {
  name: "articleDetail",
  data() {
    return {
      ArticleObj: {},
      // 偏移量
      offset: null,
      // 评论数据
      commentArr: [],
    };
  },
  components: {
    commentListVue,
  },
  methods: {
    parseTime: timeAgo,
    // 关注作者
    async follow(id, flag) {
      console.log(id);
      if (flag === true) {
        this.ArticleObj.is_followed = true;
        const res = await followingsUser(id);
        console.log(res);
        return;
      }
      // 取消关注
      else {
        this.ArticleObj.is_followed = false;

        const res = await cancelFollowingsUser(id);
        console.log(res);
        return;
      }
    },
    //点赞
    async likeArticle(flag) {
      // 点赞
      if (flag == true) {
        this.ArticleObj.attitude = 1;
        let id = this.ArticleObj.art_id;
        const res = await likeArticle({ flag, id });
        console.log(res);
      } else {
        // 取消点赞
        this.ArticleObj.attitude = 0;
        let id = this.ArticleObj.art_id;
        // 取消点赞没有数据返回
        // console.log(res);

        const res = await likeArticle({ flag, id });
      }
    },
  },
  async created() {
    // 获取文章详情
    const id = this.$route.query.art_id;
    let res = await articleDetail(id);
    this.ArticleObj = res.data.data;
    console.log(res);
  },
};
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

.van-loading {
  margin-top: 46px;
}
</style>
